package cn.itsource.hbc.pet.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.config.Point;
import cn.itsource.hbc.org.domain.Shop;
import cn.itsource.hbc.org.mapper.ShopMapper;
import cn.itsource.hbc.pet.domain.SearchMasterMsg;
import cn.itsource.hbc.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.hbc.pet.service.ISearchMasterMsgService;
import cn.itsource.hbc.user.domain.User;
import cn.itsource.utils.DistanceUtil;
import cn.itsource.utils.LoginContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service

public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
   private ShopMapper shopMapper;


    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        // 获取当前的登录用户
        User user =(User) LoginContext.getCurrent(request);
        // 将当前用户和寻找主人用户关联
        searchMasterMsg.setUser(user);
        // 用户有了之后使用工具类获取用户地址并转化为经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        // 查询一把，查出说有的门店
        List<Shop> shops = shopMapper.loadAll();
        // 将经纬度和门店作为参数传入工具类方法中：获取附近门店
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        searchMasterMsg.setShop(shop);
        searchMasterMsg.setState(0);
        searchMasterMsgMapper.save(searchMasterMsg);
    }
}
